﻿/*
 Copyright (c) 2006 - 2007 Eric J. Feminella <eric@ericfeminella.com>
 All rights reserved.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy 
 of this software and associated documentation files (the "Software"), to deal 
 in the Software without restriction, including without limitation the rights 
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished 
 to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all 
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.encryption{
	import com.adobe.crypto.MD5;

	/**
	     * 
	     * All static API which provides a Factory implementation for
	     * generating Genuine Unique Identifiers (GUID)
	     * 
	     * @example the following example demonstrates the creation of
	     * a Genuine Unique Identifiers (GUID)
	     * 
	     * <listing version="3.0">
	     * 
	     * var guid:Object = GUIDFactory.createGUID();
	     * 
	     * trace( guid );
	     * //20d9d6ae1c350d8037135cd225c513f1
	     * 
	     * </lsiting>
	     * 
	     */
	public final class GUIDFactory {
		/**
		         * 
		         * Defines a Genuine Unique Identifiers (GUID) which is 
		         * to be generated in numerical format
		         * 
		         * @example the following example demonstrates a numerical
		         * GUID
		         * 
		         * <listing version="3.0">
		         * 
		         * var guid:Object = GUIDFactory.createGUID( GUIDFactory.NUMERICAL_GUID );
		         * 
		         * trace( guid );
		         * //647477236177.8204
		         * 
		         * </lsiting>
		         * 
		         */
		public static const NUMERICAL_GUID:int=0;

		/**
		         * 
		         * Defines a Genuine Unique Identifiers (GUID) which is 
		         * to be generated in textual format
		         * 
		         * @example the following example demonstrates a textual
		         * GUID
		         * 
		         * <listing version="3.0">
		         * 
		         * var guid:Object = GUIDFactory.createGUID( GUIDFactory.TEXTUAL_GUID );
		         * 
		         * trace( guid );
		         * //240aa7cd1c16fcb20c98ae6eb824acd5.8204
		         * 
		         * </lsiting>
		         * 
		         */
		public static const TEXTUAL_GUID:int=1;

		/**
		         * 
		         * Defines the unique integer which seeds each GUID to
		         * ensure uniqueness which generating a GUID
		         *  
		         */
		private static var ID:int=0;

		/**
		         * 
		         * Creates a Genuine Unique Identifier (GUID) of the 
		         * specified type (i.e. NUMERICAL_GUID or TEXTUAL_GUID
		         * 
		         * @param  the specific type of GUID in which to create
		         * @return an object containing the guid
		         * 
		         */
		public static function createGUID(type:int=1):Object {
			var guid:Object;

			switch (type) {
				case NUMERICAL_GUID :
					guid=numericalGUID();
					break;
				case TEXTUAL_GUID :
				default :
					guid=textualGUID();
					break;
			}
			return guid;
		}

		/**
		         * 
		         * @private 
		         * 
		         * Generates a numerical Genuine Unique Identifiers (GUID)
		         * 
		         */
		private static function textualGUID():String {
			var timestamp:Number=new Date().time;
			var random:Number=Math.random()*timestamp+ID++;
			var uniqueId:String=MD5.hash(random.toString());

			return uniqueId;
		}

		/**
		         * 
		         * @private
		         * 
		         * Generates a textual Genuine Unique Identifiers (GUID)
		         * 
		         */
		private static function numericalGUID():Number {
			var timestamp:Number=new Date().time;
			var uniqueId:Number=Math.random()*timestamp+ID++;

			return uniqueId;
		}
	}
}